<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="../common/header.jsp" %>
<div class="container-fluid p-t-15">
  <div class="row">
    <div class="col-lg-12">
      <div class="card">
        <div class="card-body">
          
          <form action="#" onsubmit="return false;" method="post" class="row" id="save-form">
            
			<div class="form-group">
              <label for="example-text-input">角色名称</label>
              <input class="form-control" type="text" name="name" value="${role.name}" placeholder="请输入角色名称" required>
            </div>
            <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>
                    <label class="lyear-checkbox checkbox-primary">
                      <input name="checkbox" type="checkbox" id="check-all">
                      <span> 全选</span>
                    </label>
                  </th>
                </tr>
              </thead>
			  
              <tbody>
				<c:forEach items="${requestScope.auth}" var="menu">
				<tr>
					<td>
						<label class="lyear-checkbox checkbox-primary">
						  <c:if test="${menu.is_checked==1}">
						  <input name="auth" type="checkbox" checked class=" checkbox-parent checkbox-child" dataid="id-${menu.id}" value="${menu.id}">
						  </c:if>
						  <c:if test="${menu.is_checked==0}">
						  <input name="auth" type="checkbox" class=" checkbox-parent checkbox-child" dataid="id-${menu.id}" value="${menu.id}">
						  </c:if>
						  <span> ${menu.name}</span>
						</label>
					</td>
				</tr>
				<c:choose>
					<c:when test="${menu.children!=null}">
					<c:forEach items="${menu.children}" var="menu1">
						<tr>
						  <td class="p-l-20">
							<label class="lyear-checkbox checkbox-primary">
							<c:if test="${menu1.is_checked==1}">
							  <input name="auth" checked type="checkbox" class="checkbox-parent checkbox-child" dataid="id-${menu.id}-${menu1.id}" value="${menu1.id}">
							  </c:if>
						  <c:if test="${menu1.is_checked==0}">
						  <input name="auth" type="checkbox" class="checkbox-parent checkbox-child" dataid="id-${menu.id}-${menu1.id}" value="${menu1.id}">
						  </c:if>
							  <span> ${menu1.name}</span>
							</label>
						  </td>
						</tr>
						<c:if test="${menu1.children!=null}">
							<tr>
								<td class="p-l-40">
									<c:forEach items="${menu1.children}" var="menu2">
									<label class="lyear-checkbox checkbox-primary checkbox-inline">
									<c:if test="${menu2.is_checked==1}">
									  <input name="auth" checked type="checkbox" class=" checkbox-child " dataid="id-${menu.id}-${menu1.id}-${menu2.id}" value="${menu2.id}">
									  </c:if>
								<c:if test="${menu2.is_checked==0}">
								<input name="auth" type="checkbox" class=" checkbox-child " dataid="id-${menu.id}-${menu1.id}-${menu2.id}" value="${menu2.id}">
								</c:if>
									  <span> ${menu2.name}</span>
									</label>
									</c:forEach>
								</td>
							</tr>
						</c:if>
					</c:forEach>
					</c:when>
				</c:choose>
				</c:forEach>
              </tbody>
            </table>
          </div>
			
            <div class="form-group col-md-12">
			  <input type="hidden" name="id" value="${role.id}"/>
              <button type="submit" class="btn btn-primary ajax-post btn-sm">确 定</button>
              <button type="button" class="btn btn-default btn-sm" onclick="javascript:history.back(-1);return false;">返 回</button>
            </div>
          </form>
 
        </div>
      </div>
    </div>
    
  </div>
</div>
<%@ include file="../common/footer.jsp"%>
<script type="text/javascript">
$(function(){
    //动态选择框，上下级选中状态变化
    $('input.checkbox-parent').on('change', function(){
        var dataid = $(this).attr("dataid");
        $('input[dataid^=' + dataid + '-]').prop('checked', $(this).is(':checked'));
    });
    $('input.checkbox-child').on('change', function(){
        var dataid = $(this).attr("dataid");
            dataid = dataid.substring(0, dataid.lastIndexOf("-"));
        var parent = $('input[dataid=' + dataid + ']');
        if($(this).is(':checked')){
            parent.prop('checked', true);
            //循环到顶级
            while(dataid.lastIndexOf("-") != 2){
                dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                parent = $('input[dataid=' + dataid + ']');
                parent.prop('checked', true);
            }
        }else{
            //父级
            if($('input[dataid^=' + dataid + '-]:checked').length == 0){
                parent.prop('checked', false);
                //循环到顶级
                while(dataid.lastIndexOf("-") != 2){
                    dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                    parent = $('input[dataid=' + dataid + ']');
                    if($('input[dataid^=' + dataid + '-]:checked').length == 0){
                        parent.prop('checked', false);
                    }
                }
            }
        }
    });
});
	$("#save-form").validate({
		messages: {
			"name": {
				required: '请输入角色名称'
			}
		},
		submitHandler: function(form) {
			save();
		}
	});
	function save(){
		lightyear.loading('show');
		setTimeout(function () {
			$.ajax({
				type: "POST",//方法类型
				url: "${pageContext.request.contextPath}/admin/role?method=doEdit",
				data: $('#save-form').serialize(),
				dataType: 'json',
				success: function (result) {
					if (result.code == 200) {
						lightyear.notify(result.message, 'success', 3000);
						window.location.href="${pageContext.request.contextPath}/admin/role?method=List";
					} else {
						lightyear.notify(result.message, 'danger', 3000);
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					lightyear.notify('对不起，失败', 'danger', 3000);
				},
				complete: function () {
					lightyear.loading('hide');
				}
			});
		}, 1e3);
	}
</script>